import { request } from 'umi';

export async function addProject(data: PRO.Project, options?: Record<string, any>) {
  return request<{
    data: PRO.Project;
  }>('/api/project/add', {
    data: data,
    method: 'POST',
    ...(options || {}),
  });
}
export async function saveProject(data: PRO.Project, options?: Record<string, any>) {
  return request<{
    data: PRO.Project;
  }>('/api/project/save', {
    data: data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function saveRunner(data: PRO.Runner, options?: Record<string, any>) {
  return request<{
    data: PRO.Runner;
  }>('/api/runner/save', {
    data: data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function projectList(params: PRO.Page, options?: Record<string, any>) {
  return request<PRO.PageResponse>('/api/project/list', {
    params: params,
    method: 'GET',
    ...(options || {}),
  });
}

export async function runnerList(params: PRO.Page, options?: Record<string, any>) {
  return request<PRO.PageResponse>('/api/runner/list', {
    params: params,
    method: 'GET',
    ...(options || {}),
  });
}

export async function serverList(params: PRO.Page, options?: Record<string, any>) {
  return request<PRO.PageResponse>('/api/server/list', {
    params: params,
    method: 'GET',
    ...(options || {}),
  });
}

export async function projectSelectList(options?: Record<string, any>) {
  return request<
    {
      project_name: string;
      id: number;
    }[]
  >('/api/project/selectList', {
    method: 'GET',
    ...(options || {}),
  });
}

export async function serverSelectList(options?: Record<string, any>) {
  return request<
    {
      server_name: string;
      id: number;
    }[]
  >('/api/server/selectList', {
    method: 'GET',
    ...(options || {}),
  });
}

export async function commitLogSelectList(projectId: number, options?: Record<string, any>) {
  return request<
    {
      commit_id: string;
      author: string;
      date: string;
      comment: string;
    }[]
  >('/api/project/commitList', {
    params: {
      project_id: projectId,
    },
    method: 'GET',
    ...(options || {}),
  });
}

export async function diffLog(
  pamras: {
    deploy_id?: number;
    project_id?: number;
    commit_id?: string;
    page?: number;
    page_size?: number;
  },
  options?: Record<string, any>,
) {
  return request<PRO.PageResponse>('/api/deploy/diff', {
    params: pamras,
    method: 'GET',
    ...(options || {}),
  });
}

export async function createDeploy(
  data: {
    project_id: number;
    commit_id: string;
    deploy_comment: string;
  },
  options?: Record<string, any>,
) {
  return request<PRO.GitDiff>('/api/deploy/create', {
    data: data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function deployLog(
  data: {
    project_id?: number;
    deploy_comment: string;
    status: any;
    page: number;
    page_size: number;
  },
  options?: Record<string, any>,
) {
  return request<PRO.PageResponse>('/api/deploy/list', {
    params: data,
    method: 'GET',
    ...(options || {}),
  });
}

export async function deployLogData(
  data: {
    id: number;
  },
  options?: Record<string, any>,
) {
  return request<PRO.DeployLog>('/api/deploy/log', {
    data: data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function savePublish(data: any, options?: Record<string, any>) {
  return request<PRO.PageResponse>('/api/server/save', {
    data: data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function publish(data: { deploy_id: number }, options?: Record<string, any>) {
  return request<PRO.PageResponse>('/api/deploy/publish', {
    data: data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function getDeployConfig(projectId: number, options?: Record<string, any>) {
  return request<PRO.DeployLog>('/api/deploy/DeployConfig', {
    params: {
      project_id: projectId,
    },
    method: 'GET',
    ...(options || {}),
  });
}

export async function saveServerConfig(
  serverConfig: PRO.ServerSetting,
  options?: Record<string, any>,
) {
  return request<PRO.DeployLog>('/api/server/saveServer', {
    data: serverConfig,
    method: 'POST',
    ...(options || {}),
  });
}

export async function getLabels(options?: Record<string, any>) {
  return request<
    {
      label: PRO.Label;
      runners: PRO.Runner[];
    }[]
  >('/api/project/labels', {
    params: {},
    method: 'GET',
    ...(options || {}),
  });
}

export async function checkServerIsRun(serverId: number, options?: Record<string, any>) {
  return request<
    {
      label: PRO.Label;
      runners: PRO.Runner[];
    }[]
  >('/api/server/alive', {
    params: { server_id: serverId },
    method: 'GET',
    ...(options || {}),
  });
}

export async function getConfig(options?: Record<string, any>) {
  return request('/api/config', {
    method: 'GET',
    ...(options || {}),
  });
}
